package com.indsvr.common.utils.sql;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public final class JdbcUtils {
    private final Connection connection;

    private JdbcUtils(Connection connection) {
        this.connection = connection;
    }

    //获取链接
    public static JdbcUtils getConnection(String url, String user, String pwd) throws Exception {
        Connection connection = HikariUtils.getConnection(url,user,pwd);
        JdbcUtils jdbcUtils = new JdbcUtils(connection);
        return jdbcUtils;
    }

    public <T> List<T> select(String sql, Class<T> clazz) throws Exception {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            return analysis(resultSet, clazz);
        } finally {
            assert statement != null;
            statement.close();
            assert resultSet != null;
            resultSet.close();
        }
    }

    public List<HashMap<String,Object>> select(String sql) throws Exception {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            return analysis0(resultSet);
        } finally {
            assert statement != null;
            statement.close();
            assert resultSet != null;
            resultSet.close();
        }
    }

    //归还链接
    public void repayment() throws Exception{
        connection.close();
    }

    @Deprecated
    private List<HashMap<String,Object>> analysis0(ResultSet resultSet) throws Exception {
        int columnsCount = 0;
        boolean f = false;
        if (!resultSet.next()) {
            return new ArrayList<>();
        }
        List<HashMap<String, Object>> hashMaps = new ArrayList<>();
        ResultSetMetaData rsmd = resultSet.getMetaData();
        columnsCount = rsmd.getColumnCount();//数据集的列数
        while (!f) {
            HashMap<String, Object> hashMap = new HashMap<>();
            for (int i = 1; i <= columnsCount; i++) {
                String columnLabel = rsmd.getColumnLabel(i);
                Object object = resultSet.getObject(columnLabel);
                hashMap.put(columnLabel, object);
            }
            if (!resultSet.next()) {
                f = true;
            }
            hashMaps.add(hashMap);
        }

        return hashMaps;
    }

    private <T> List<T> analysis(ResultSet resultSet, Class<T> tClass) throws Exception {

        int columnsCount = 0;
        boolean f = false;
        if (!resultSet.next()) {
            return new ArrayList<>();
        }
        List<T> hashMaps = new ArrayList<>();
        ResultSetMetaData rsmd = resultSet.getMetaData();
        columnsCount = rsmd.getColumnCount();//数据集的列数
        while (!f) {
            T t = tClass.getDeclaredConstructor().newInstance();
            for (int i = 1; i <= columnsCount; i++) {
                String columnLabel = rsmd.getColumnLabel(i);
                Object object = resultSet.getObject(columnLabel);
                Field declaredField = tClass.getDeclaredField(columnLabel);
                declaredField.setAccessible(true);  //无视访问修饰符
                declaredField.set(t, object);
            }
            if (!resultSet.next()) {
                f = true;
            }
            hashMaps.add(t);
        }

        return hashMaps;
    }


}